package com.zonta.finance.serivce.impl;


import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.common.utils.DateUtil;
import com.zonta.finance.config.ObjectConvertor;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.CreateVoucherWordConfigForm;
import com.zonta.finance.domain.form.DelVoucherWordConfigForm;
import com.zonta.finance.domain.form.UpdateVoucherWordConfigDefaultFlagForm;
import com.zonta.finance.domain.form.UpdateVoucherWordConfigForm;
import com.zonta.finance.domain.po.VoucherWordConfig;
import com.zonta.finance.domain.vo.GetVoucherWordConfigVo;
import com.zonta.finance.domain.vo.ListVoucherWordConfigVo;
import com.zonta.finance.mapper.VoucherWordConfigMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.serivce.IVoucherWordConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 凭证字配置 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-30
 */
@Service
@RequiredArgsConstructor
public class VoucherWordConfigServiceImpl extends ServiceImpl<VoucherWordConfigMapper, VoucherWordConfig> implements IVoucherWordConfigService {
    private final TokenService<AdminDTO> tokenService;
    private final TransactionTemplate transactionTemplate;
    private final ObjectConvertor objectConvertor;
    /**
     * 添加凭证字
     * @param form
     * @return
     */
    @Override
    public Boolean createByFrom(CreateVoucherWordConfigForm form) {
        if (ObjUtil.isNotEmpty(getByVoucherWord(form.getVoucherWord()))){
            throw new BizException("凭证字已存在");
        }
        Boolean execute = transactionTemplate.execute(transactionStatus -> {
            VoucherWordConfig insertVoucherWordConfig = objectConvertor.toVoucherWordConfig(form);
            insertVoucherWordConfig.initDefault();
            insertVoucherWordConfig.setMemberId(tokenService.getThreadLocalUserId());
            insertVoucherWordConfig.setTenantId(tokenService.getThreadLocalTenantId());
            return save(insertVoucherWordConfig);
        });
        return execute==null? false:execute;
    }

    /**
     * 删除凭证字
     * @param form
     * @return
     */
    @Override
    public Boolean delById(DelVoucherWordConfigForm form) {
        LambdaUpdateWrapper<VoucherWordConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VoucherWordConfig::getDelFlag,true)
                .set(VoucherWordConfig::getUpdateMemberId,tokenService.getThreadLocalUserId())
                .set(VoucherWordConfig::getUpdateTime, DateUtil.getSystemTime())
                .eq(VoucherWordConfig::getId,form.getId())
                .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
        return update(wrapper);
    }

    /**
     * 修改凭证字
     * @param form
     * @return
     */
    @Override
    public Boolean updateByFrom(UpdateVoucherWordConfigForm form) {
        VoucherWordConfig voucherWord = getByVoucherWord(form.getVoucherWord());
        if (ObjUtil.isNotEmpty(voucherWord)){
            throw new BizException("凭证字已存在");
        }
        LambdaUpdateWrapper<VoucherWordConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VoucherWordConfig::getVoucherWord,form.getVoucherWord())
                .set(VoucherWordConfig::getPrintTitle,form.getPrintTitle())
                .set(VoucherWordConfig::getUpdateMemberId,tokenService.getThreadLocalUserId())
                .set(VoucherWordConfig::getUpdateTime,new Date())
                .eq(VoucherWordConfig::getId,form.getId())
                .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
        return update(wrapper);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @Override
    public GetVoucherWordConfigVo getGetVoucherWordConfigVoById(Long id) {
        LambdaQueryWrapper<VoucherWordConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VoucherWordConfig::getId,id)
                .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
        VoucherWordConfig voucherWordConfig = getOne(wrapper);
        return objectConvertor.toGetVoucherWordConfigVo(voucherWordConfig);
    }

    /**
     * 查询凭证字列表
     * @return
     */
    @Override
    public List<ListVoucherWordConfigVo> lists() {
        LambdaQueryWrapper<VoucherWordConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(VoucherWordConfig::getId,VoucherWordConfig::getVoucherWord,VoucherWordConfig::getPrintTitle,VoucherWordConfig::getDefaultFlag,VoucherWordConfig::getUseCount);
        wrapper.eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
        List<VoucherWordConfig> list = list(wrapper);
        return objectConvertor.toListVoucherWordConfigVo(list);

    }
    /**
     *修改默认凭证
     * @param form
     * @return
     */
    @Override
    public Boolean updateDefaultFlag(UpdateVoucherWordConfigDefaultFlagForm form) {
        transactionTemplate.execute(transactionStatus->{
            if (!clearDefault()){
                throw new BizException("修改默认凭证失败");
            }
            LambdaUpdateWrapper<VoucherWordConfig> wrapper = new LambdaUpdateWrapper<>();
            wrapper.set(VoucherWordConfig::getDefaultFlag,false)
                    .set(VoucherWordConfig::getMemberId,tokenService.getThreadLocalUserId())
                    .set(VoucherWordConfig::getUpdateTime,new Date())
                    .eq(VoucherWordConfig::getId,form.getId())
                    .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
            boolean flag = update(wrapper);
            if (flag){
                throw new BizException("修改失败");
            }
            return true;
        });
        return true;
    }

    /**
     * 查询凭证字信息
     *
     * @param voucherWord
     * @return
     */
    private VoucherWordConfig getByVoucherWord(String voucherWord) {
        LambdaQueryWrapper<VoucherWordConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(VoucherWordConfig::getId,VoucherWordConfig::getVoucherWord,VoucherWordConfig::getPrintTitle)
                .eq(VoucherWordConfig::getVoucherWord,voucherWord)
                .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());

        return getOne(wrapper);
    }

    /**
     * 清空默认凭证字
     *
     * @return
     */
    private boolean clearDefault() {
        LambdaUpdateWrapper<VoucherWordConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(VoucherWordConfig::getDefaultFlag,false)
                .set(VoucherWordConfig::getUpdateMemberId,tokenService.getThreadLocalUserId())
                .set(VoucherWordConfig::getUpdateTime,new Date())
                .eq(VoucherWordConfig::getTenantId,tokenService.getThreadLocalTenantId());
        boolean flag = update(wrapper);
        return flag;
    }
}
